package com.ruoyi.cms.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cms.domain.CmsSiteItem;
import com.ruoyi.cms.service.ICmsSiteItemService;
import com.ruoyi.common.constant.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CmsSiteArticle;
import com.ruoyi.cms.service.ICmsSiteArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 站点文章Controller
 *
 * @author ruoyi
 * @date 2022-08-23
 */
@RestController
@RequestMapping("/cms/article")
public class CmsSiteArticleController extends BaseController {
    @Autowired
    private ICmsSiteArticleService cmsSiteArticleService;
    @Autowired
    private ICmsSiteItemService cmsSiteItemService;

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('cms:article:list')")
    @GetMapping("/items")
    public AjaxResult litems() {
        CmsSiteItem cmsSiteItem = new CmsSiteItem();
        cmsSiteItem.setStatus(Constants.SUCCESS);
        List<CmsSiteItem> list = cmsSiteItemService.selectCmsSiteItemList(cmsSiteItem);
        List<Map<String, Object>> items = new ArrayList<>();
        handItemInfo(list, items, "", 0L);
        return AjaxResult.success(items);
    }

    private void handItemInfo(List<CmsSiteItem> src, List<Map<String, Object>> target, String pName, Long pid) {
        for (CmsSiteItem item : src) {
            if (pid.equals(item.getPid())) {
                Map<String, Object> map = new HashMap<>();
                String label = item.getName();
                if (StringUtils.isNotBlank(pName)) {
                    label = pName + " > " + item.getName();
                }
                map.put("value", item.getId());
                map.put("label", label);
                target.add(map);
                handItemInfo(src, target, label, item.getId());
            }
        }
    }

    /**
     * 查询站点文章列表
     */
    @PreAuthorize("@ss.hasPermi('cms:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsSiteArticle cmsSiteArticle) {
        startPage();
        List<CmsSiteArticle> list = cmsSiteArticleService.selectCmsSiteArticleList(cmsSiteArticle);
        return getDataTable(list);
    }

    /**
     * 导出站点文章列表
     */
    @PreAuthorize("@ss.hasPermi('cms:article:export')")
    @Log(title = "站点文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsSiteArticle cmsSiteArticle) {
        List<CmsSiteArticle> list = cmsSiteArticleService.selectCmsSiteArticleList(cmsSiteArticle);
        ExcelUtil<CmsSiteArticle> util = new ExcelUtil<CmsSiteArticle>(CmsSiteArticle.class);
        util.exportExcel(response, list, "站点文章数据");
    }

    /**
     * 获取站点文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cmsSiteArticleService.selectCmsSiteArticleById(id));
    }

    /**
     * 新增站点文章
     */
    @PreAuthorize("@ss.hasPermi('cms:article:add')")
    @Log(title = "站点文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsSiteArticle cmsSiteArticle) {
        return toAjax(cmsSiteArticleService.insertCmsSiteArticle(cmsSiteArticle));
    }

    /**
     * 修改站点文章
     */
    @PreAuthorize("@ss.hasPermi('cms:article:edit')")
    @Log(title = "站点文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsSiteArticle cmsSiteArticle) {
        return toAjax(cmsSiteArticleService.updateCmsSiteArticle(cmsSiteArticle));
    }

    /**
     * 删除站点文章
     */
    @PreAuthorize("@ss.hasPermi('cms:article:remove')")
    @Log(title = "站点文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsSiteArticleService.deleteCmsSiteArticleByIds(ids));
    }
}
